package cn.itcast.web.controller;

import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class LoginController extends BaseController{

    @Autowired
    private UserService userService;
    @Autowired
    private ModuleService moduleService;

    /** 1. 普通的登陆认证
    @RequestMapping("/login")
    public String login(String email,String password){
        // 非空判断
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(password)){
            return "forward:/login.jsp";
        }

        // 根据email查询
        User user = userService.findByEmail(email);

        // 判断email是否存在、密码校验
        if (user != null && user.getPassword().equals(password)){
            // 登陆成功,跳转到主页 main.jsp
            session.setAttribute("loginInfo",user);
            // 查询用户的权限并保存，页面显示动态菜单数据
            List<Module> moduleList = moduleService.findModuleByUserId(user.getId());
            session.setAttribute("moduleList",moduleList);
            return "home/main";
        }

        // 登陆失败
        request.setAttribute("error","用户名或密码错误！");
        return "forward:/login.jsp";
    }*/


    /** 2. shiro的登陆认证*/
    @RequestMapping("/login")
    public String login(String email,String password){
        // 非空判断
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(password)){
            return "forward:/login.jsp";
        }

        try {
            // 创建一个token，封装·用户输入·的账号密码
            AuthenticationToken token = new UsernamePasswordToken(email,password);
            // 创建subject，表示当前用户
            Subject subject = SecurityUtils.getSubject();
            // shiro登陆认证, 执行login()认证方法，会自动去到realm的认证方法中
            subject.login(token);

            // 获取身份对象: subject.getPrincipal() 获取的是realm中认证方法返回的对象的构造函数的第一个参数
            User user = (User) subject.getPrincipal();

            // 登陆认证成功
            // A. 存储用户到session
            session.setAttribute("loginInfo",user);
            // B. 查询登陆用户的权限 (动态菜单)
            List<Module> moduleList = moduleService.findModuleByUserId(user.getId());
            session.setAttribute("moduleList",moduleList);
            // C. 跳转
            return "home/main";
        } catch (AuthenticationException e) {
            e.printStackTrace();
            // 登陆失败
            request.setAttribute("error","用户名或密码错误！");
            return "forward:/login.jsp";
        }
    }

    @RequestMapping("/home")
    public String home(){
        return "home/home";
    }

    /**
     * 退出
     */
    @RequestMapping("/logout")
    public String logout(){
        // 先释放资源
        session.removeAttribute("loginInfo");
        // 清空shiro的认证信息
        SecurityUtils.getSubject().logout();
        // 再销毁session
        session.invalidate();
        return "forward:/login.jsp";
    }
}
